from time import sleep
from threading import Thread,Lock

## 转账案例
# class Account(object):
#
#     def __init__(self):
#         self._balance = 0
#         self._lock = Lock()
#
#     def deposit(self, money):
#         # 先获得锁
#         self._lock.acquire()
#
#         try:
#             # 计算存款后的余额
#             new_balance = self._balance + money
#             # 模拟受理存款业务需要0.01秒的时间
#             sleep(0.01)
#             # 修改账户余额
#             self._balance = new_balance
#         finally:
#             self._lock.release() # 释放锁
#
#     @property
#     def balance(self):
#         return self._balance
#
#
# class AddMoneyThread(Thread):
#
#     def __init__(self, account, money):
#         super().__init__()
#         self._account = account
#         self._money = money
#
#     def run(self):
#         self._account.deposit(self._money)
#
#
# def main():
#     account = Account()
#     threads = []
#     # 创建100个存款的线程向同一个账户中存钱
#     for _ in range(100):
#         t = AddMoneyThread(account, 1)
#         threads.append(t)
#         t.start()
#     # 等所有存款的线程都执行完毕
#     for t in threads:
#         t.join()
#     print('账户余额为: ￥%d元' % account.balance)


##
import time
import tkinter
import tkinter.messagebox
from os import getpid
import threading
# def download():
#     # 模拟下载任务需要花费10秒钟时间
#     time.sleep(10)
#     tkinter.messagebox.showinfo('提示', '下载完成!')

class Download_Tnew(Thread):

    def __init__(self):
        super().__init__()
        pass

    def run(self):
        print('开始下载...')
        # print('pid is :{}'.format(getpid())) # 打印进程信息
        print('pid is :{}'.format(threading.enumerate())) # 打印线程信息

        time.sleep(10)
        tkinter.messagebox.showinfo('提示', '下载完成!')



def download():
    Download_Tnew().start()

def show_about():
    tkinter.messagebox.showinfo('关于', '作者: 骆昊(v1.0)')


def main():
    top = tkinter.Tk()
    top.title('单线程')
    top.geometry('200x150')
    top.wm_attributes('-topmost', True)

    panel = tkinter.Frame(top)

    # 增加线程

    button1 = tkinter.Button(panel, text='下载', command=download)
    button1.pack(side='left')
    button2 = tkinter.Button(panel, text='关于', command=show_about)
    button2.pack(side='right')
    panel.pack(side='bottom')

    tkinter.mainloop()


if __name__ == '__main__':
    main()